/**
 *@ClassName SelfAjax
 *@Description  自定义ajax
 *@Author JiJiang
 *@Date 2022/9/26 19:00
 *@Version 1.0
 */

function MyAjax(url, type, data = {}, headers = {}) {
    let retData = null;
    $.ajax({
        url: url,
        type: type,
        data: data,
        // headers: headers,
        async: false,//取消异步
        dataType: "json",  //指定返回的数据类型格式
        beforeSend: function (xhr) {
            // console.log("请求头codeKey === ",headers.codeKey);
            if (headers != null) {
                if (headers.hasOwnProperty("codeKey")) {
                    xhr.setRequestHeader("codeKey", headers.codeKey);
                }
            }

            let token = localStorage.getItem("token");
            if (token != null && token != undefined) {
                xhr.setRequestHeader("token", token);
            }
        },
        success: function (result, textStatus, response) {
            retData = result;
            //获取响应头中的token   --->这里的token再登录后就保存到本地不会发生变化
            let token = response.getResponseHeader('token');
            // console.log("response === ",response);
            // console.log("token === ",token);
            if (token != null && token !== undefined) {
                retData.token = token;
            }
        }
    });

    return retData;
}


